package com.yunze.system.card.apisyn;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yunze.apiCommon.utils.InternalApiRequest;
import com.yunze.apiCommon.utils.VeDate;
import com.yunze.common.mapper.mysql.YzCardMapper;

import com.yunze.common.utils.yunze.CardFlowSyn;
import com.yunze.common.utils.yunze.GetShowStatIdArr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 卡用量 生命周期 X 分钟补偿 消费者
 */
@Slf4j
@Component
public class CardFlowAndStats {


    @Resource
    private InternalApiRequest internalApiRequest;
    @Resource
    private CardFlowSyn cardFlowSyn;
    @Resource
    private YzCardMapper yzCardMapper;
    @Resource
    private GetShowStatIdArr getShowStatIdArr;




    /**
     * 用量补偿 - 【详情查看】
     * @param msg
     * @param channel_1
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = "compensate_CardFlow_queue",containerFactory = "customContainerFactory")
    public void CompensateCardFlow(String msg, Channel channel_1) {
        if(msg!=null && msg.length()>0){
            synCardFlow(msg);
        }
    }

    /**
     * 死信队列  用量补偿 - 【详情查看】
     * @param msg
     * @param channel_1
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = "dlx_compensate_CardFlow_queue",containerFactory = "customContainerFactory")
    public void DlxCompensateCardFlow(String msg, Channel channel_1)  {
        if(msg!=null && msg.length()>0){
            synCardFlow(msg);
        }
    }


    /**
     * 生命周期补偿 - 【详情查看】
     * @param msg
     * @param channel_1
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = "compensate_CardStasus_queue",containerFactory = "customContainerFactory")
    public void CompensateCardStasus(String msg, Channel channel_1) {
        if(msg!=null && msg.length()>0){
            synCardStatus(msg);
        }
    }

    /**
     * 死信队列  生命周期补偿 - 【详情查看】
     * @param msg
     * @param channel_1
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = "dlx_compensate_CardStasus_queue",containerFactory = "customContainerFactory")
    public void DlxCompensateCardStasus(String msg, Channel channel_1)  {
        if(msg!=null && msg.length()>0){
            synCardStatus(msg);
        }
    }
    


    /**
     * 同步卡用量
     *
     * @param msg
     * @throws IOException
     */
    public void synCardFlow(String msg) {
        String iccid = "";
        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String, Object> map = JSON.parseObject(msg);
            iccid = map.get("iccid").toString();
                Map<String, Object> Parammap = new HashMap<>();
                Parammap.put("iccid", iccid);
                Map<String, Object> Rmap = internalApiRequest.queryFlow(Parammap, map);
                String code = Rmap.get("code") != null ? Rmap.get("code").toString() : "500";
                if (code.equals("200")) {
                    //获取 卡用量 开卡日期 更新 card info
                    if (Rmap.get("Use") != null && Rmap.get("Use") != "" && Rmap.get("Use").toString().trim().length() > 0) {
                        Double Use = Double.parseDouble(Rmap.get("Use").toString());
                        if (Use >= 0) {
                            try {
                                Map<String, Object> RMap = cardFlowSyn.CalculationFlow(iccid, Use);
                                log.info(">>cardFlowSyn - 卡用量补偿 同步卡用量返回:| {} | {} | {} <<",  iccid, JSON.toJSON(RMap), JSON.toJSON(Rmap));
                            } catch (Exception e) {
                                log.error(">>cardFlowSyn - 卡用量补偿 同步卡用量失败: {} | {}  | {}<<",  iccid, JSON.toJSON(Rmap) , e.getMessage().toString());
                            }
                        } else {
                            log.info(">>API - 卡用量补偿 未获取到卡用量 {} |  statusCode = 0 :{} | {}<<",  iccid, Rmap);
                        }
                    }
                } else {
                    log.info(">>API - 卡用量补偿 未获取到卡用量:{} | {} | {}<<",  iccid, Rmap);
                }
        } catch (Exception e) {
            log.error(">>错误 - 卡用量补偿:{}<<", e.getMessage());
        }

       
    }



    /**
     * 同步卡状态
     *
     * @param msg
     * @throws IOException
     */
    public void synCardStatus(String msg) {
        String iccid = "";
        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String, Object> map = JSON.parseObject(msg);
            iccid = map.get("iccid").toString();
            Map<String, Object> Parammap = new HashMap<>();
            Parammap.put("iccid", iccid);
            Map<String, Object> Rmap = internalApiRequest.queryCardStatus(Parammap, map);
            String code = Rmap.get("code") != null ? Rmap.get("code").toString() : "500";
            if (code.equals("200")) {
                //获取 卡状态 开卡日期 更新 card info
                if (Rmap.get("statusCode") != null && Rmap.get("statusCode") != "" && Rmap.get("statusCode").toString().trim().length() > 0) {
                    String statusCode = Rmap.get("statusCode").toString().trim();
                    if (!statusCode.equals("0")) {
                        Map<String, Object> Upd_Map = new HashMap<>();
                        Upd_Map.put("status_id", statusCode);
                        Upd_Map.put("iccid", iccid);
                        try {
                            Object status_id = Upd_Map.get("status_id");
                            String status_ShowId = getShowStatIdArr.GetShowStatId(status_id.toString());
                            Upd_Map.put("status_ShowId",status_ShowId);
                            int upd = yzCardMapper.updStatusId(Upd_Map);
                            log.info(">>发送 admin_CardUpdStatusId_queue 指令成功 ！ iccid:{} | upd:{} | Upd_Map {}<<", iccid,upd, Upd_Map);

                        } catch (Exception e) {
                            log.error(">>发送 admin_CardUpdStatusId_queue 失败 ！ iccid:{} | Upd_Map {}  e ： {}<<", iccid, Upd_Map, e.getMessage());
                        }
                    } else {
                        log.info(">>API - 卡状态补偿消费者 未获取到卡状态 statusCode = 0 :{} | {}<<", iccid, Rmap);
                    }
                }
            } else {
                log.info(">>API - 卡状态补偿消费者 未获取到卡状态:{} | {}<<", iccid, Rmap);
            }
            //}
        } catch (Exception e) {
            log.error(">>错误 - 卡状态补偿消费者:{}<<", e.getMessage());
        }


    }



}
